/**
 * Created by user on 2017/3/1.
 */
import React from 'react';
import {Form, Input, Button, Modal, Message, DatePicker, Icon, Table, Select} from 'antd';
const FormItem = Form.Item;
const confirm = Modal.confirm;

let RoomList = React.createClass({
  contextTypes: {
    router: React.PropTypes.object
  },

  getInitialState() {
    return {
      formsData: {},
      data: [],
      pagination: {
        pageSize: 10,
        current: 1
      },
      startTime: null,
      visible: false
    }
  },
  
  componentDidMount() {
    this.handleSearch();
  },
  
  handleTableChange(pagination) {
    const pager = this.state.pagination;
    pager.current = pagination.current;
    this.setState({
      pagination: pager
    });
    this.fetch();
  },
  
  
  fetch() {
    let forms = this.props.form.getFieldsValue();
    let page = this.state.pagination.current;
    forms.startTime = Utils.formatDate( forms.startTime);
    forms.endTime = Utils.formatDate( forms.endTime);
    let params = Object.assign({}, {pageSize: 10, currentPage: page}, forms);
    let _me = this
    Utils.sendAjax({
      url: '/wechat/user/reserveRoom/list',
      data: JSON.stringify(params),
      callback: function(result) {
        if (result.code == '200') {
          _me.setState({
            data: result.data,
            pagination: result.page
          })
        } else {
          Message.error(result.message, 10)
        }
      }
    })
  },
  rowKey(data) {
    return data.id;
  },
  
  handleSearch() {
    this.setState({
      pagination: {
        current: 1
      }
    })
    this.fetch();
  },
  
  handleReset() {
    this.props.form.resetFields();
  },
  

  handleModify(record) {
      let id = record.id;
      let me = this;
      confirm({
          title: '租客已来看房？',
          onOk() {
            Utils.sendAjax({
              url: '/wechat/user/reserveRoom/confirm',
              data: JSON.stringify({id: id}),
              callback: (result)=> {
                if(result.code == '200') {
                 me.fetch();
                }else {

                }
              }
            })
          },
        onCancel() {

        }

      })
    me.setState({
      visible:　true
    })

  },

  
  
  handleSignUp(record) {
    let params = {
      roomId: record.roomId
    };
    let goPathUrl= '/landlord/rentManage/advanceOrder/add';
    this.context.router.push({pathname: goPathUrl, state: {...params}});
  },


  handleTime(val) {
    this.setState({
      startTime: val
    })
  },
  
  render() {
    const { getFieldProps } = this.props.form;
    // console.log(this.props.form)
    let _me = this;
    const columns = [
      
      {
        title: '房间地址',
        dataIndex: 'address'
      },
      {
        title: '房间名',
        dataIndex: 'roomName',
      },
      {
        title: '租客姓名',
        dataIndex: 'contactUser'
      },
      {
        title: '联系方式',
        dataIndex: 'contactPhone'
      },
      {
        title: '预约看房时间',
        dataIndex: 'reserveTime'
      },
      {
        title: '租金',
        dataIndex: 'rent'
      },
      {
        title: '是否看房',
        render: (text, record) => {
          return (
            <span>{record.status == 1 ? '是' : '否'}</span>
          )
        }
      },
      {
        title: '创建预约时间',
        dataIndex: 'createTime'
      },
      {
        title: '操作',
        render: (text, record) => {
          return (
            
            <div>
              <div>
                <a href="javascript:;" onClick={this.handleModify.bind(this, record)} disabled={record.status == 1 ? true : false}>看房</a>&nbsp;|&nbsp;
                <a href="javascript:;"  onClick={this.handleSignUp.bind(this, record)}>预订</a>
              </div>

            </div>
          )
        }
      },
    ];
    const disabledDate = function (current) {
      let key = _me.state.startTime.getTime();
      return current && current.getTime() < key;
    };

    return (
      <div>
        <div className="search-bar">
          <Form inline>
            <FormItem  label="关键字">
              <Input {...getFieldProps('keyWord') } placeholder="关键字"/>
            </FormItem>
            <FormItem  label="是否看房">
              <Select {...getFieldProps('status') } style={{width: 162}} placeholder="是否看房">
                <Option value="1">是</Option>
                <Option value="2">否</Option>
              </Select>
            </FormItem>
            <FormItem label="预约时间">
              <DatePicker  {...getFieldProps('startTime',{onChange: this.handleTime})} placeholder="预约起始日期" style={{width: 162}}/>&nbsp;至&nbsp;
              <DatePicker  {...getFieldProps('endTime')} placeholder="预约结束日期" style={{width: 162}}/>
            </FormItem>
            <FormItem>
              <Button type="primary" onClick={this.handleSearch} style={{marginRight: 20}}>查询</Button>
              <Button type="default" onClick={this.handleReset} >重置</Button>
            </FormItem>
          </Form>
        </div>
        <Table rowKey={this.rowKey} columns={columns} dataSource={this.state.data}
               onChange={this.handleTableChange}  pagination={this.state.pagination}/>
      </div>
    );
  }
});
RoomList = Form.create()(RoomList);
export default RoomList